﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace MDIDemo.Modules
{
    public partial class HeThong : DevExpress.XtraEditors.XtraForm
    {
        int idSua = 27;
        public frmMain Main = null;
        void SetPermission()
        {
            btnSua.Visible = frmMain.IsPermis(idSua, Main.CurrentPermis.PERMISSION);
            btnLuu.Visible = frmMain.IsPermis(idSua, Main.CurrentPermis.PERMISSION);
        }
        public HeThong()
        {
            InitializeComponent();

        }

        private void HeThong_Load(object sender, EventArgs e)
        {
            EnabledForm(false);
            BUS.CommonControl ctr = new BUS.CommonControl();
            DataTable dt = ctr.executeSelectQuery("Select * from HETHONG");
            if (dt.Rows.Count > 0)
            {
                txtDiaChi.Text = dt.Rows[0]["DiaChi"].ToString();
                txtDienThoai.Text = dt.Rows[0]["DienThoai"].ToString();
                txtFax.Text = dt.Rows[0]["Fax"].ToString();
                txtGiamDoc.Text = dt.Rows[0]["GiamDoc"].ToString();
                txtKeToanTruong.Text = dt.Rows[0]["KeToanTruong"].ToString();
                txtMaSoThue.Text = dt.Rows[0]["MaSoThue"].ToString();
                txtNganHang.Text = dt.Rows[0]["NganHang"].ToString();
                txtTaiKhoan.Text = dt.Rows[0]["TaiKhoan"].ToString();
                txtTen.Text = dt.Rows[0]["TenDV"].ToString();
                txtTinhTP.Text = dt.Rows[0]["TinhTP"].ToString();
            }
            SetPermission();
        }

        private void EnabledForm(bool p)
        {
            txtDiaChi.Properties.ReadOnly = !p;
            txtDienThoai.Properties.ReadOnly = !p;
            txtFax.Properties.ReadOnly = !p;
            txtGiamDoc.Properties.ReadOnly = !p;
            txtKeToanTruong.Properties.ReadOnly = !p;
            txtMaSoThue.Properties.ReadOnly = !p;
            txtNganHang.Properties.ReadOnly = !p;
            txtTaiKhoan.Properties.ReadOnly = !p;
            txtTen.Properties.ReadOnly = !p;
            txtTinhTP.Properties.ReadOnly = !p;
            btnLuu.Enabled = p;
            btnSua.Enabled = !p;
            btnBoQua.Enabled = p;
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            EnabledForm(true);
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            BUS.CommonControl ctr = new BUS.CommonControl();
            try
            {
                ctr.executeNonQuery("Delete  from HETHONG");
                ctr.executeNonQuery(string.Format("Insert into HETHONG(TenDV,DiaChi,DienThoai,Fax,MaSoThue,GiamDoc,KeToanTruong,TaiKhoan,NganHang,TinhTP) values(N'{0}',N'{1}',N'{2}',N'{3}',N'{4}',N'{5}',N'{6}',N'{7}',N'{8}',N'{9}')"
                    , txtTen.Text, txtDiaChi.Text, txtDienThoai.Text, txtFax.Text, txtMaSoThue.Text, txtGiamDoc.Text, txtKeToanTruong.Text, txtTaiKhoan.Text, txtNganHang.Text, txtTinhTP.Text));
                XtraMessageBox.Show("Lưu thành công", "Thông báo");
            }
            catch (Exception ex) { XtraMessageBox.Show(ex.Message, "Lỗi"); }
            EnabledForm(false);
        }

        private void btnBoQua_Click(object sender, EventArgs e)
        {
            EnabledForm(false);
        }

        private void simpleButton5_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void HeThong_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                if (btnBoQua.Enabled)
                    btnBoQua_Click(null, null);
            }
            else if (e.KeyCode == Keys.F7)
            {
                if (btnLuu.Enabled)
                    btnLuu_Click(null, null);
            }
            else if (e.KeyCode == Keys.F2)
            {
                if (btnSua.Enabled)
                    btnSua_Click(null, null);
            }
         
        }
    }
}